<template>
  <div>
    <el-dialog
      title="添加商品"
      :visible.sync="dialogVisible"
      width="60%"
      :before-close="handleClose"
    >
      <el-table ref="showShopTable" :data="shops" stripe @selection-change="selectionShop">
        <el-table-column type="selection"></el-table-column>
        <el-table-column prop="id" label="编号"></el-table-column>
        <el-table-column prop="shopName" label="商品名称"></el-table-column>
        <el-table-column prop="shopPrice" label="商品价格"></el-table-column>
        <el-table-column prop="type.name" label="商品类型"></el-table-column>
        <el-table-column prop="shopUnit" label="商品规格"></el-table-column>
        <el-table-column>
          <template slot="header" slot-scope="scope" width="300">
            <el-input
              v-model="shop.shopName"
              size="mini"
              @keyup.enter.native="search"
              placeholder="输入商品名搜索">
            </el-input>
          </template>
        </el-table-column>
      </el-table>
      <div>
        <!-- 分页 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageno"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addShops">确认</el-button>
        <el-button @click="closeDialog">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: "ShowShops",
    data() {
      return {
        dialogVisible: false,//模态框状态
        shops: [],
        shop: {id: "", shopNumber: "", shopName: "", shopPrice: "", shopAddr: "", isUp: ""},
        pageno: 1,
        pagesize: 5,
        total: 1,
        chekedShops: [],//已选择种商品的数组
      }
    },
    methods: {
      addShops() {
        //获取要添加的商品
        if (this.chekedShops.length > 0) {
          //去重复添加到表单
          this.chekedShops.forEach(item => {
            let t = this.$parent.addForm.find(item2=>item2.id==item.id);
            if(!t){
              var obj = {
                id: item.id,
                shopNumber: item.shopNumber,
                shopName: item.shopName,
                type: item.type,
                shopPrice:item.shopPrice,//商品单价
                supplyOrderId: "",//供应商商品维护表id
                supplyId: "",//供应商id
                supplierPrice: 0,//供应商提供的价格
                shopNum: 1, //采购数量
                uname: ""//供应商
              };
              this.$parent.addForm.push(obj);
            }
          });
          this.$message.success("添加成功")
          this.$refs.showShopTable.clearSelection();//清空选择
          this.dialogVisible = false;
        } else {
          this.$message.error("请选中商品")
        }
      },
      getShopData() {
        //组装数据
        var params = new URLSearchParams();
        params.append("pageno", this.pageno);
        params.append("pagesize", this.pagesize);
        Object.keys(this.shop).forEach(item => {
          params.append(item, this.shop[item]);
        })
        this.$axios.get("shop/shop.action", {params: params})
          .then(res => {
            this.shops = res.data.records;
            this.total = res.data.total;
          })
          .catch(err => {
            alert(err)
          })
      },
      search() {//按商品名称搜索
        this.getShopData();
      },
      handleSizeChange(val) {
        //每页范围
        this.pagesize = val;
        this.getShopData();
      },
      handleCurrentChange(val) {
        //当前页
        this.pageno = val;
        this.getShopData();
      },
      handleClose(done) {
        //模态框的关闭
        this.$confirm("确认关闭？")
          .then((_) => {
            done();
          })
          .catch((_) => {
          });
      },
      selectionShop(val) {
        //checkbox选项表改变获取对象数组
        this.chekedShops = val;
      },
      closeDialog() {
        //取消模态框
        this.dialogVisible = false;
      }
    }
    ,
    created() {
      this.getShopData();
    }
  }
</script>

<style scoped>

</style>
